package com.study.lxy.methodhandler;

import java.io.PrintStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;

public class MethodHandlerTest {

    private static MethodHandle getMethodHandler(Object target, String funcName) throws Exception{
        MethodType methodType = MethodType.methodType(void.class, String.class);
        return MethodHandles.lookup().findVirtual(target.getClass(), funcName, methodType).bindTo(target);
    }

    public static void main(String[] args) throws Throwable {
        getMethodHandler(System.out, "println").invokeExact("liuxingyi");
        getMethodHandler(new MethodHandlerTest(), "println").invokeExact("liuxingyi");

        Class<MethodHandlerTest> methodHandlerTestClass = MethodHandlerTest.class;
        MethodHandlerTest methodHandlerTest = methodHandlerTestClass.newInstance();
        methodHandlerTest.println("aaaaaaaaaa");


    }

    public void println(String str){
        System.out.println("haha " + str);
    }
}
